package com.kh.samr.service.impl;

import com.kh.samr.dto.FindListByStartingTimeDto;
import com.kh.samr.dto.ParamPageDto;
import com.kh.samr.entity.expert.ExpertInfo;
import com.kh.samr.repository.IExpertInfoRepository;
import com.kh.samr.service.IExpertInfoService;
import com.kh.samr.utils.ResultDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 专家基本信息业务实现类
 *
 * @author Harry
 * @date 2020/12/28
 */
@Service
@Transactional
public class ExpertInfoServiceImpl implements IExpertInfoService {
	
	private static final Logger log = LoggerFactory.getLogger(ExpertInfoServiceImpl.class);
	
	@Resource
	IExpertInfoRepository expertInfoRepository;
	
	@Override
	public ResultDto<List<ExpertInfo>> findAllExpertInfo(ParamPageDto paramPageDto) {
		log.info("方法名称：findAllExpertInfo;参数：" + null);
		ResultDto<List<ExpertInfo>> result = new ResultDto<>();
		PageRequest pageRequest = this.buildPageRequest(paramPageDto.getPageNum(), paramPageDto.getPageSize());
		Page<ExpertInfo> expertInfoPage = this.expertInfoRepository.findAll(pageRequest);
		result.setErrcode(0);
		result.setList(expertInfoPage.getContent());
		result.setTotal(expertInfoPage.getTotalElements());
		log.info("方法名称：findAllExpertInfo;返回值：" + result);
		return result;
	}
	
	@Override
	public ResultDto<List<ExpertInfo>> findExpertInfosByCreatTime(FindListByStartingTimeDto findListByStartingTimeDto) {
		log.info("方法名称：findExpertInfosByCreatTime;参数：findListByStartingTimeDto:" + findListByStartingTimeDto.toString());
		ResultDto<List<ExpertInfo>> result = new ResultDto<>();
		Specification<ExpertInfo> specification = (root, query, cb) -> {
			List<Predicate> list = new ArrayList<>();
			Predicate p;
			if (findListByStartingTimeDto.getStartTime() != null) {
				p = cb.greaterThanOrEqualTo(root.get("creatTime").as(Date.class), findListByStartingTimeDto.getStartTime());
				list.add(p);
			}
			
			Predicate[] pre = new Predicate[list.size()];
			return query.where(list.toArray(pre)).getRestriction();
		};
		PageRequest pageRequest = this.buildPageRequest(findListByStartingTimeDto.getPageNum(), findListByStartingTimeDto.getPageSize());
		Page<ExpertInfo> expertInfoPage = this.expertInfoRepository.findAll(specification, pageRequest);
		result.setErrcode(0);
		result.setList(expertInfoPage.getContent());
		result.setTotal(expertInfoPage.getTotalElements());
		log.info("方法名称：findExpertInfosByCreatTime;返回值：" + result);
		return result;
	}
	
	/**
	 * 构建分页参数
	 *
	 * @param pageNumber 当前页数
	 * @param pageSize   每页条数
	 * @return 分页参数
	 */
	private PageRequest buildPageRequest(int pageNumber, int pageSize) {
		return PageRequest.of(pageNumber - 1, pageSize);
	}
}
